package com.gxa.ehome.web.servlet;

import com.alibaba.fastjson.JSON;
//import com.gxa.ehome.entity.Coupon;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxa.ehome.entity.Cp;
import com.gxa.ehome.service.CpService;
import com.gxa.ehome.service.impl.CpServiceImpl;
import org.apache.poi.hssf.usermodel.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ExportServlet",urlPatterns = "/export")
public class ExportServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 先从request域中获取数据
//        String jsonString = "[{\"id\": \"1\",\"name\": \"冬日优惠券\",\"desc\":\"满100减10\",\"colletExpire\": \"2020-5-1 12:12:12\",\"useExpire\": \"2021-12-31 12:12:12\",\"role\":\"优惠券详细使用规则:1.准时 2.线下使用 3.过期不补\",\"img\":\"/images/upload/034dc92dc3b74a45b4703847dd2f71de.png\"},{\"id\": \"2\",\"name\": \"暖心优惠券\",\"desc\":\"满100减10\",\"colletExpire\": \"2020-1-1 12:12:12\",\"useExpire\": \"2021-12-31 12:12:12\",\"role\":\"优惠券详细使用规则:1.准时2.线下使用3.过期不补\",\"img\":\"/images/upload/034dc92dc3b74a45b4703847dd2f71de.png\"}]";
        System.out.println("sb");
        CpService cpService=new CpServiceImpl();
        List<Cp> cpList=null;
        try {
            cpList = cpService.selectAllCp();

        } catch (Exception e) {
            e.printStackTrace();
        }
        String jsonString  = JSON.toJSON(cpList).toString();

        System.out.println(jsonString);

        //工作簿对象
        List<Cp> all = JSON.parseArray(jsonString, Cp.class);
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作表
        HSSFSheet sheet = workbook.createSheet("优惠券列表");
        String fileName = "优惠券列表.xls";
        //将文件名中的中文部分被URL编码后的内容进行解析,最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName,"utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "序号", "名称", "使用规则","注意事项","领用有效期","使用有效期","是否上架"};
        //创建一个行对象,作为表头
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);    //在行内添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);    //把字段名添加到表头行上
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 添加数据行:
        int rowNum = 1;
        for (Cp item : all) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getDesc());
            dataRow.createCell(3).setCellValue(item.getRole());
            dataRow.createCell(4).setCellValue(dateFormat.format(item.getColletExpire()));
            dataRow.createCell(5).setCellValue(dateFormat.format(item.getUseExpire()));
            dataRow.createCell(6).setCellValue(item.isInUse());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }
}
